﻿using System;
using System.Collections.Generic;
using System.IO;
using FileMerge.Helpers;
using FileMerge.Models;

namespace FileMerge
{
    class Program
    {
        static int baseDirectoryIndex = 0;

        static void Main(string[] args)
        {
            Console.WriteLine("FileMerge 0.1");
            Console.WriteLine("=======================================================================================");

            if (ConfigHelper.Configs == null || ConfigHelper.Configs.Count == 0)
            {
                Console.WriteLine($"Failed to load configs.");
                Console.ReadKey();

                return;
            }

            foreach (var config in ConfigHelper.Configs)
            {
                baseDirectoryIndex = config.BaseDirectory.Length;

                Console.WriteLine($"config: {config.Path}");
                Console.WriteLine($"base directory: {config.BaseDirectory}");

                PrintResult(MergeHelper.Merge(config));

                Console.WriteLine("=======================================================================================");
            }

            Console.ReadKey();
        }

        static void PrintResult(MergeResult result)
        {
            Console.WriteLine("Result: " + (result.Succeed ? "Succeed" : "Failed"));
            Console.WriteLine();

            Console.WriteLine($"{result.Message}");
            Console.WriteLine();

            Console.WriteLine("--------------------------------------------");
            Console.WriteLine("Merged Files:");
            Console.WriteLine();

            foreach (var item in result.Merged)
            {
                Console.WriteLine(item.Substring(baseDirectoryIndex));
            }
            Console.WriteLine();

            Console.WriteLine("--------------------------------------------");
            Console.WriteLine("Ignored Files:");
            Console.WriteLine();

            foreach (var item in result.Ignored)
            {
                Console.WriteLine(item.Substring(baseDirectoryIndex));
            }
            Console.WriteLine();

            Console.WriteLine("--------------------------------------------");
            Console.WriteLine("Failed Files:");
            Console.WriteLine();

            foreach (var item in result.Failed)
            {
                Console.WriteLine(item.Substring(baseDirectoryIndex));
            }
            Console.WriteLine();
        }
    }
}
